package com.mty.kjtest;

import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.StrictMode;
import android.util.Log;
import com.mty.kjtest.Configure.Config;
import com.mty.kjtest.Error.DefaultUncaughtHandler;

/**
 * Created by 天宇 on 2017/6/29.
 */
public class App extends Application {

    protected static final String TAG = App.class.getSimpleName();
    protected static App instance;

    public static synchronized App getInstance() {
        return instance;
    }




    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
        if (Config.catchUncaughtException) {
            //设置报错界面
            Thread.setDefaultUncaughtExceptionHandler(createUncaughtExceptionHandler());
        }
        if (Config.strictMode) {
            //严格模式
            setStrictMode();
        }
    }


    protected void setStrictMode() {
        Log.i(TAG, "start strict mode ... ");
        StrictMode.ThreadPolicy threadPolicy = new StrictMode
                .ThreadPolicy.Builder().detectAll().penaltyLog().penaltyDropBox().build();
        StrictMode.setThreadPolicy(threadPolicy);

        StrictMode.VmPolicy vmPolicy = new StrictMode
                .VmPolicy.Builder().detectAll().penaltyLog().penaltyDropBox().build();
        StrictMode.setVmPolicy(vmPolicy);
    }


    protected Thread.UncaughtExceptionHandler createUncaughtExceptionHandler() {
        Log.i(TAG, "set uncaught handler ... ");
        return new DefaultUncaughtHandler() {

            @Override
            protected void handleException(Thread t, Throwable e) {
            }
        };
    }

    public void onHomeActCreate(Activity homeActivity) {


        //checkNewVersion(homeActivity);// 检测最新版本
    }


    /**
     * 检查新版本
     */
    private void checkNewVersion(final Activity activity) {
        PackageInfo packInfo = null;
        try {
            packInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packInfo != null) {
            final String localVercode = packInfo.versionName;
            //调用接口检查版本更新然后更新版本

            //
            //doNewVerUp(false, activity, apkurl, netVersion, VersionContent);// 更新新版本
        }
    }


    /**
     * 更新新版本
     * must  是否必须  如果是则不会出现取消键
     * apkurl apk的下载地址
     * netVersion 新版本的版本等级
     * content 更新内容
     */
    private void doNewVerUp(boolean must, final Activity activity, final String apkurl,
                            final String netVersion, String content) {

    }

}
